namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineLinearRegressionChannel : PaintLineBase
    {
        public PaintLineLinearRegressionChannel()
        {
            base.LineType = PaintLineType.LinearRegressionChannel;
            base.PlName_zh = "线性回归通道";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                PaintLineBase.PointValue value2 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = base.PointList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num4 = base.CgInstance.GetX(num2);
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                double[] numArray = this.LineParam();
                base.CgInstance.Cursor = Cursors.Hand;
                base.MoveType = 0;
                if (numArray != null)
                {
                    double num5 = numArray[0];
                    double num6 = numArray[1];
                    double chartValue = num5 + num6;
                    double num8 = (((num2 - pos) + 1) * num5) + num6;
                    float num9 = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
                    float num10 = base.CgInstance.GetY(base.OwnedPanelID, num8, null);
                    if (((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num9 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num9 + (base.LineWidth * 2.5f)))))
                    {
                        base.MoveType = 1;
                        base.CgInstance.Cursor = Cursors.SizeWE;
                    }
                    if (((crossHairPoint.X >= (num4 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num4 + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num10 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num10 + (base.LineWidth * 2.5f)))))
                    {
                        base.MoveType = 2;
                        base.CgInstance.Cursor = Cursors.SizeWE;
                    }
                    int num11 = 0;
                    foreach (BaseShape shape in div.ShapeDic.Values)
                    {
                        if (shape.ShapeType == ShapeType.Candle)
                        {
                            CandleShape shape2 = shape as CandleShape;
                            if (!shape2.CrossFlag)
                            {
                                num11++;
                            }
                        }
                    }
                    if (((num11 >= 2) && (base.BsName != null)) && (base.BsName.Length > 0))
                    {
                        int num12 = pos + ((num2 - pos) / 2);
                        float num13 = base.CgInstance.GetX(num12);
                        double num14 = (((num12 - pos) + 1) * num5) + num6;
                        float num15 = base.CgInstance.GetY(base.OwnedPanelID, num14, null);
                        if (((crossHairPoint.X >= (num13 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num13 + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num15 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num15 + (base.LineWidth * 2.5f)))))
                        {
                            base.MoveType = 3;
                            base.CgInstance.Cursor = Cursors.SizeNS;
                        }
                    }
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                PaintLineBase.PointValue value2 = pList[0];
                double num1 = value2.Value;
                PaintLineBase.PointValue value3 = pList[1];
                double num30 = value3.Value;
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num2 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float num3 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num4 = base.CgInstance.GetX(num2) - base.CgInstance.LeftYScaleWidth;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                double[] numArray = this.LineParam(pList);
                if (base.Selected)
                {
                    g.DrawLine(pen, num3, 0f, num3, (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID));
                    g.DrawLine(pen, num4, 0f, num4, (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID));
                }
                if (numArray != null)
                {
                    double num5 = numArray[0];
                    double num6 = numArray[1];
                    double chartValue = num5 + num6;
                    double num8 = (((num2 - pos) + 1) * num5) + num6;
                    float num9 = (base.CgInstance.GetY(base.OwnedPanelID, chartValue, null) - div.Rect.Y) - div.TitleHeight;
                    float num10 = (base.CgInstance.GetY(base.OwnedPanelID, num8, null) - div.Rect.Y) - div.TitleHeight;
                    g.DrawLine(pen, num3, num9, num4, num10);
                    double[] parallelParam = this.GetParallelParam(pList);
                    double num11 = chartValue + parallelParam[0];
                    double num12 = num8 + parallelParam[0];
                    double num13 = chartValue - parallelParam[1];
                    double num14 = num8 - parallelParam[1];
                    float num15 = (base.CgInstance.GetY(base.OwnedPanelID, num11, null) - div.Rect.Y) - div.TitleHeight;
                    float num16 = (base.CgInstance.GetY(base.OwnedPanelID, num12, null) - div.Rect.Y) - div.TitleHeight;
                    float num17 = (base.CgInstance.GetY(base.OwnedPanelID, num13, null) - div.Rect.Y) - div.TitleHeight;
                    float num18 = (base.CgInstance.GetY(base.OwnedPanelID, num14, null) - div.Rect.Y) - div.TitleHeight;
                    g.DrawLine(pen, num3, num15, num4, num16);
                    g.DrawLine(pen, num3, num17, num4, num18);
                    int num19 = 0;
                    foreach (BaseShape shape in div.ShapeDic.Values)
                    {
                        if (shape.ShapeType == ShapeType.Candle)
                        {
                            CandleShape shape2 = shape as CandleShape;
                            if (!shape2.CrossFlag)
                            {
                                num19++;
                            }
                        }
                    }
                    if (((num2 - pos) >= 2) && (num19 >= 2))
                    {
                        int num20 = pos + ((num2 - pos) / 2);
                        float num21 = base.CgInstance.GetX(num20) - base.CgInstance.LeftYScaleWidth;
                        double num22 = (((num20 - pos) + 1) * num5) + num6;
                        float num23 = (base.CgInstance.GetY(base.OwnedPanelID, num22, null) - div.Rect.Y) - div.TitleHeight;
                        if (base.Selected)
                        {
                            g.FillRectangle(brush, new RectangleF(num21 - 2.5f, num23 - 2.5f, 5f, 5f));
                        }
                    }
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    pen.DashPattern = new float[] { 3f, 2f };
                    num8 = ((base.CgInstance.LastVisibleRecord - pos) * num5) + num6;
                    float num24 = ((base.CgInstance.LastVisibleRecord - base.CgInstance.FirstVisibleRecord) * base.CgInstance.XScaleSpace) + (base.CgInstance.XScaleSpace / 2f);
                    double num25 = num8 + parallelParam[0];
                    double num26 = num8 - parallelParam[1];
                    float num27 = (base.CgInstance.GetY(base.OwnedPanelID, num8, null) - div.Rect.Y) - div.TitleHeight;
                    float num28 = (base.CgInstance.GetY(base.OwnedPanelID, num25, null) - div.Rect.Y) - div.TitleHeight;
                    float num29 = (base.CgInstance.GetY(base.OwnedPanelID, num26, null) - div.Rect.Y) - div.TitleHeight;
                    g.DrawLine(pen, num4, num16, num24, num28);
                    g.DrawLine(pen, num4, num18, num24, num29);
                    g.DrawLine(pen, num4, num10, num24, num27);
                    if (base.Selected)
                    {
                        g.FillRectangle(brush, new RectangleF(num3 - 2.5f, num9 - 2.5f, 5f, 5f));
                        g.FillRectangle(brush, new RectangleF(num4 - 2.5f, num10 - 2.5f, 5f, 5f));
                    }
                }
                pen.Dispose();
                brush.Dispose();
            }
        }

        public double[] GetParallelParam()
        {
            return this.GetParallelParam(base.PointList);
        }

        public double[] GetParallelParam(List<PaintLineBase.PointValue> pList)
        {
            CandleShape shape = base.CgInstance.GetShape(base.BsName) as CandleShape;
            if (shape != null)
            {
                double[] numArray = this.LineParam(pList);
                if (numArray != null)
                {
                    double num = numArray[0];
                    double num2 = numArray[1];
                    List<double> valueList = new List<double>();
                    List<double> list2 = new List<double>();
                    PaintLineBase.PointValue value2 = pList[0];
                    int num3 = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                    PaintLineBase.PointValue value3 = pList[1];
                    int num4 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                    for (int i = num3; i <= num4; i++)
                    {
                        if (!(base.CgInstance.DataSource.Rows[i][shape.HighField] is DBNull) && !(base.CgInstance.DataSource.Rows[i][shape.LowField] is DBNull))
                        {
                            double num6 = Convert.ToDouble(base.CgInstance.DataSource.Rows[i][shape.HighField]);
                            double num7 = Convert.ToDouble(base.CgInstance.DataSource.Rows[i][shape.LowField]);
                            double num8 = (((i - num3) + 1) * num) + num2;
                            valueList.Add(num6 - num8);
                            list2.Add(num8 - num7);
                        }
                    }
                    double highValue = Common.GetHighValue(valueList);
                    double num10 = Common.GetHighValue(list2);
                    return new double[] { highValue, num10 };
                }
            }
            return null;
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                foreach (BaseShape shape in div.ShapeDic.Values)
                {
                    if ((shape.ShapeType == ShapeType.Candle) && (shape.ZOrder == 0))
                    {
                        CandleShape shape2 = shape as CandleShape;
                        if (!shape2.CrossFlag)
                        {
                            int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                            if ((mouseOverIndex >= 0) && (mouseOverIndex <= (base.CgInstance.LastVisibleRecord - 1)))
                            {
                                int num3 = mouseOverIndex;
                                int num4 = num3 - 1;
                                if (num4 >= 0)
                                {
                                    DataRow row = base.CgInstance.DataSource.Rows[num4];
                                    DataRow row2 = base.CgInstance.DataSource.Rows[num3];
                                    string key = row[base.CgInstance.CalenderScaleField].ToString();
                                    string str2 = row2[base.CgInstance.CalenderScaleField].ToString();
                                    base.PointList.Clear();
                                    double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                                    base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                                    base.PointList.Add(new PaintLineBase.PointValue(1, str2, valueByPoint));
                                    base.BsName = shape.Name;
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                PaintLineBase.PointValue value2 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = base.PointList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                base.CgInstance.GetX(pos);
                base.CgInstance.GetX(num2);
                ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                double[] numArray = this.LineParam();
                if (numArray != null)
                {
                    double num3 = numArray[0];
                    double num4 = numArray[1];
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    if ((mouseOverIndex >= pos) && (mouseOverIndex <= (base.CgInstance.LastVisibleRecord - 1)))
                    {
                        double[] parallelParam = this.GetParallelParam();
                        double chartValue = (num3 * ((mouseOverIndex - pos) + 1)) + num4;
                        float num7 = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
                        float x = base.CgInstance.GetX(mouseOverIndex);
                        if (((crossHairPoint.X >= (x - 5f)) && (crossHairPoint.X <= (x + 5f))) && ((crossHairPoint.Y >= (num7 - 5f)) && (crossHairPoint.Y <= (num7 + 5f))))
                        {
                            return true;
                        }
                        chartValue = ((num3 * ((mouseOverIndex - pos) + 1)) + num4) + parallelParam[0];
                        num7 = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
                        x = base.CgInstance.GetX(mouseOverIndex);
                        if (((crossHairPoint.X >= (x - 5f)) && (crossHairPoint.X <= (x + 5f))) && ((crossHairPoint.Y >= (num7 - 5f)) && (crossHairPoint.Y <= (num7 + 5f))))
                        {
                            return true;
                        }
                        chartValue = ((num3 * ((mouseOverIndex - pos) + 1)) + num4) - parallelParam[1];
                        num7 = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
                        x = base.CgInstance.GetX(mouseOverIndex);
                        if (((crossHairPoint.X >= (x - 5f)) && (crossHairPoint.X <= (x + 5f))) && ((crossHairPoint.Y >= (num7 - 5f)) && (crossHairPoint.Y <= (num7 + 5f))))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            CandleShape shape = base.CgInstance.GetShape(base.BsName) as CandleShape;
            if (shape != null)
            {
                PaintLineBase.PointValue value2 = pList[0];
                int num = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = pList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                if ((num != -1) && (num2 != -1))
                {
                    List<double> vList = new List<double>();
                    for (int i = num; i <= num2; i++)
                    {
                        if (!(base.CgInstance.DataSource.Rows[i][shape.CloseField] is DBNull))
                        {
                            vList.Add(Convert.ToDouble(base.CgInstance.DataSource.Rows[i][shape.CloseField]));
                        }
                    }
                    return Common.LinearRegressionEquation(vList);
                }
            }
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    PaintLineBase.PointValue value2 = base.PointList[0];
                    int num3 = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                    PaintLineBase.PointValue value3 = base.PointList[1];
                    int num4 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num5 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num6 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num7 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num8 = (num5 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num7 - num6);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num10 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            int num11 = (base.CgInstance.GetRecordByTime(value4.Key) + (num10 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            int num12 = (base.CgInstance.GetRecordByTime(value5.Key) + (num10 - indexByPoint)) - 1;
                            if (num11 >= 0)
                            {
                                if (num11 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num11 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num11 = 0;
                            }
                            if (num12 < 0)
                            {
                                num12 = 0;
                            }
                            else if (num12 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num12 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            if (num12 != num11)
                            {
                                PaintLineBase.PointValue value6 = base.BeginMoveValue[0];
                                base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num11][base.CgInstance.CalenderScaleField].ToString(), value6.Value + num8);
                                PaintLineBase.PointValue value7 = base.BeginMoveValue[1];
                                base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num12][base.CgInstance.CalenderScaleField].ToString(), value7.Value + num8);
                            }
                            return;
                        }
                        case 1:
                            if (mouseOverIndex < num4)
                            {
                                base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            }
                            return;

                        case 2:
                            if (mouseOverIndex > num3)
                            {
                                base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            }
                            return;

                        case 3:
                        {
                            CandleShape shape = base.CgInstance.GetShape(base.BsName) as CandleShape;
                            if (shape != null)
                            {
                                double num13 = Convert.ToDouble(base.CgInstance.DataSource.Rows[mouseOverIndex][shape.LowField]);
                                double num14 = Convert.ToDouble(base.CgInstance.DataSource.Rows[mouseOverIndex][shape.HighField]);
                                if ((mouseOverValue < num13) || (mouseOverValue > num14))
                                {
                                    foreach (BaseShape shape2 in div.ShapeDic.Values)
                                    {
                                        if ((shape2 != shape) && (shape2.ShapeType == ShapeType.Candle))
                                        {
                                            CandleShape shape3 = shape2 as CandleShape;
                                            if (!shape3.CrossFlag)
                                            {
                                                num13 = Convert.ToDouble(base.CgInstance.DataSource.Rows[mouseOverIndex][shape3.LowField]);
                                                num14 = Convert.ToDouble(base.CgInstance.DataSource.Rows[mouseOverIndex][shape3.HighField]);
                                                if ((mouseOverValue >= num13) && (mouseOverValue <= num14))
                                                {
                                                    base.BsName = shape3.Name;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            return;
                        }
                    }
                }
            }
        }
    }
}

